const {createRole, getRoleList, getRoleOne, updateRole, delRole, getRoleListAll} = require('../service/role')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')

const getRole = async ({name = '', isSchool, description = '', pageIndex = 0, pageSize = 10, createTime = []}) => {
  const result = await getRoleList({name, isSchool, description, pageIndex, pageSize, createTime})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getRoleById = async (id) => {
  let result = await getRoleOne(+id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const patchRole = async ({name, isSchool, auth, description, id}) => {
  const result = await updateRole({name, auth, isSchool: !!isSchool, description, id})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const deleteRole = async (id) => {
  const result = await delRole(id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const postRole = async ({name, auth, isSchool, description}) => {
  const role = await createRole({name, auth, isSchool: !!isSchool, description})

  try {
    return new SuccessModel(role)
  } catch (e) {
    return new ErrorModel(addRoleFailInfo)
  }
}

const getRoleAll = async () => {
  const result = await getRoleListAll()
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

module.exports = {
  postRole,
  getRole,
  getRoleById,
  patchRole,
  deleteRole,
  getRoleAll
}
